<?php

namespace App\Console\Commands;

use App\Models\Category;
use App\Models\Notification;
use App\Models\Order;
use App\Services\SmsService;
use Illuminate\Console\Command;

class NearSmsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'near:sms';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = date('Y-m-d');
        Order::where('sms_date', $date)->join('users', 'orders.user_id', 'users.id')->chunk(10, function ($orders) {
            foreach ($orders as $order) {
                $extra = [
                    'merchant_id' =>  $order->merchant_id,
                    'to_user_id' => $order->user_id
                ];
                SmsService::near($order->phone, $extra);

                $date = date('m月d日', strtotime($order->date));
                $productName = Category::find($order->category_id)->title;
                //生成通知
                Notification::create([
                    'merchant_id' => $order->merchant_id,
                    'message' => $order->username . '消费者，电话号码是'.$order->phone.'，于'.$date.'购买了'.$order->buy_num.'听并赠送了'
                        .$order->give_num.'听'.$productName.'产品，还有2天就使用完了，请跟进服务。'
                ]);

            }
        });
    }
}
